import Socket from './lib/wsUtil.js'

const input = document.querySelector('#input');
const send = document.querySelector('#send');
const app = document.querySelector('#app');




send.addEventListener('click', (e) => {

    console.log(e);

    const message = input.value;

    if (message) {
        testConnect(message);
    } else {
        alert('请输入链接地址');
    }

});


function testConnect(val) {
    new Socket({
        url: val,
        timer: 10000,
        // 断线重连
        reconnect: true,
        onopen: (e) => {

            console.log('连接成功:', e);

            appendChild(e);
        },
        onmessage: (e) => {
            console.log('接收到消息:', e);
            appendChild(e);
        },
    });
}

function appendChild(e) {
    const date = new Date();
    let newE = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()} \n\n ${e}`;
    const area = document.createElement("textarea");
    const br = document.createElement("br")
    area.innerText = newE;
    app.appendChild(area);
    app.appendChild(br);
}